package com.tan.simpleweather.ui.place

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.tan.simpleweather.R
import com.tan.simpleweather.base.AppDataBase
import com.tan.simpleweather.base.PlaceBean
import com.tan.simpleweather.logic.model.Place
import com.tan.simpleweather.ui.weather.WeatherActivity
import com.tan.simpleweather.ui.weather.WeatherListActivity
import kotlinx.android.synthetic.main.activity_weather.*
import retrofit2.http.PUT
import kotlin.concurrent.thread

/**

@author： TanJun
@date： 2021/11/15 11:00
@description： TODO
@modifiedBy：
@version: 1.0
 */
class PlaceAdapter(private val fragment: PlaceFragment, private val placeList: List<Place>):
    RecyclerView.Adapter<PlaceAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val placeName  = view.findViewById<TextView>(R.id.placeName)
        val placeAddress = view.findViewById<TextView>(R.id.placeAddress)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.place_item, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            val pos = holder.adapterPosition
            val place = placeList[pos]
            val mActivity = fragment.activity
            mActivity.let { it ->
                if (it is WeatherActivity){
                    it.drawerLayout.closeDrawers()
                    with(place){
                        it.viewModel.locationLng = location.lng
                        it.viewModel.locationLat = location.lat
                        it.viewModel.placeName = name
                        it.refreshWeather()
                    }
                }else{
                    Intent(parent.context, WeatherListActivity::class.java).let {
                        it.putExtra("location_lng", place.location.lng)
                        it.putExtra("location_lat", place.location.lat)
                        it.putExtra("place_name", place.name)
                        it.putExtra("place_address", place.address)
                        it.putExtra("from_adapter",true)
                        fragment.startActivity(it)
                    }
                    it?.finish()
                }
            }
            fragment.viewModel.savePlace(place)
            /** insert data */
           /* thread {
                val placeBean = PlaceBean(place.name, place.location.lng, place.location.lat, place.address)
                val id =  AppDataBase.getDataBase(parent.context).getPlace().insertPlace(placeBean)
            }*/
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(placeList[position]){
            holder.placeName.text = name
            holder.placeAddress.text = address
        }
    }

    override fun getItemCount(): Int = placeList.size

}